package mobi.hami.huapin.service.impl;

/**
 * Create by 子龙 on 2018/09/05.
 */
import mobi.hami.huapin.base.Result;
import mobi.hami.huapin.dao.UserOrderMapper;
import mobi.hami.huapin.dao.UserReturnGoodsLogMapper;
import mobi.hami.huapin.dao.UserWechatMapper;
import mobi.hami.huapin.dto.UserOrderDTO;
import mobi.hami.huapin.model.UserInvoice;
import mobi.hami.huapin.model.UserReturnGoodsLog;
import mobi.hami.huapin.model.UserWechat;
import mobi.hami.huapin.service.ReturnGoodsService;
import mobi.hami.huapin.util.Dates;
import mobi.hami.huapin.util.Results;
import mobi.hami.huapin.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class ReturnGoodsServiceImpl implements ReturnGoodsService {

    private static final Logger logger = LoggerFactory.getLogger(ReturnGoodsServiceImpl.class);

    @Autowired
    private UserReturnGoodsLogMapper userReturnGoodsLogMapper;

    @Autowired
    private UserOrderMapper userOrderMapper;

    @Autowired
    private UserWechatMapper userWechatMapper;

    /**
     * 退货申请(退货原因,退货图片,订单状态大于等于5才能退货)
     * @param reqMap
     * @return
     */
    public Result AddreturnGoods(Map<String,Object> reqMap){

        Object user_id_obj = reqMap.get("user_id");
        if(null == user_id_obj || StringUtil.isEmpty(user_id_obj.toString())){
            return Results.failureWithStatus(2,"user_id is null");

        }
        Integer user_id = new Integer(user_id_obj.toString());

        Object open_id_obj = reqMap.get("open_id");
        if(null == open_id_obj || StringUtil.isEmpty(open_id_obj.toString())){
            return Results.failureWithStatus(3,"open_id is null");
        }
        String open_id = String.valueOf(open_id_obj);

        UserWechat uwc = userWechatMapper.selectByWechatSmallId(open_id);
        if(null == uwc){
            return Results.failureWithStatus(4,"该用户不存在");
        }
        if(!uwc.getUser_id().equals(user_id)){
            return Results.failureWithStatus(5,"用户信息不一致");
        }

        Object order_id_obj = reqMap.get("order_id");
        if(null == order_id_obj || StringUtil.isEmpty(order_id_obj.toString())){
            return Results.failureWithStatus(6,"order_id is null");
        }
        Integer order_id = new Integer(String.valueOf(order_id_obj));

        Object reason_obj = reqMap.get("reason");
        if(null == reason_obj || StringUtil.isEmpty(reason_obj.toString())){
            return Results.failureWithStatus(7,"reason is null");
        }
        String reason = String.valueOf(reason_obj);

        Object imgs_obj = reqMap.get("imgs");
        if(null == imgs_obj || StringUtil.isEmpty(imgs_obj.toString())){
            return Results.failureWithStatus(8,"imgs is null");
        }
        String imgs = String.valueOf(imgs_obj);

        UserOrderDTO userOrderDTO = userOrderMapper.getOneById(order_id);
        if(null == userOrderDTO){
            return Results.failureWithStatus(9,"该订单不存在");
        }
        if(!userOrderDTO.getUser_id().equals(user_id) ){
            return  Results.failureWithStatus(10,"用户信息不一致,暂不支持退货");
        }
        if(userOrderDTO.getOrder_status() < 5){
            return  Results.failureWithStatus(11,"该订单未收货或者派发,暂不支持退货");
        }
        if(userOrderDTO.getOrder_status() >= 5){
            if(Dates.addHours(userOrderDTO.getUpdate_time(),7*24,null,null).after(new Date())){
                UserReturnGoodsLog urlong = userReturnGoodsLogMapper.find(user_id,open_id,order_id);
                if(null != urlong){
                    return  Results.failureWithStatus(12,"该订单已申请过退货");
                }
            }else {
                return  Results.failureWithStatus(13,"该订单不在退货时间内");
            }
        }
        UserReturnGoodsLog  urgl = new UserReturnGoodsLog(user_id,open_id,order_id,null,null,null,null,reason,imgs,new Date(),null,null,null,0);
        int i = userReturnGoodsLogMapper.insert(urgl);
        if(i > 0){
            return Results.successWithStatus(0,"添加退货信息成功");
        }
        return Results.failureWithStatus(1,"添加退货信息失败");
    }

    /**
     *填写退货方式信息(退货申请记录id,快递机构id,快递单号,退货人称谓,退货人联系方式)  注:前提退货申请处于同意状态
     * @param reqMap
     * @return
     */
    public Result updateReturnGoods(Map<String,Object> reqMap){
        /**
         * the validation of UserReturnGoodsLog's id
         */
        Object the_order_id_obj = reqMap.get("the_order_id");
        if(null == the_order_id_obj || StringUtil.isEmpty(String.valueOf(the_order_id_obj))){
            return Results.failureWithStatus(2,"the id of UserReturnGoodsLog is null");
        }
        Integer the_order_id = new Integer(String.valueOf(the_order_id_obj));
        /**
         * the validation of user_id
         */
        Object user_id_obj = reqMap.get("user_id");
        if(null == user_id_obj || StringUtil.isEmpty(user_id_obj.toString())){
            return Results.failureWithStatus(5,"user_id is null");
        }
        Integer user_id = new Integer(user_id_obj.toString());
        /**
         * the validation of open_id
         */
        Object open_id_obj = reqMap.get("open_id");
        if(null == open_id_obj || StringUtil.isEmpty(open_id_obj.toString())){
            return Results.failureWithStatus(6,"open_id is null");
        }
        String open_id = String.valueOf(open_id_obj);
        /**
         * the validation of UserReturnGoodsLog
         */
        UserReturnGoodsLog userReturnGoodsLog = userReturnGoodsLogMapper.find(user_id,open_id,the_order_id);
        if(null == userReturnGoodsLog){
            return Results.failureWithStatus(3,"the UserReturnGoodsLog is null");
        }
        /**
         * the validation of UserReturnGoodsLog's status is 1  注:受理退货状态0:申请中1:同意退货2:拒绝退货3:退货完成
         */
        if(userReturnGoodsLog.getStatus() != 1){
            return Results.failureWithStatus(4,"the UserReturnGoodsLog's status is not 1");
        }

        /**
         * the validation of user
         */
        UserWechat uwc = userWechatMapper.selectByWechatSmallId(open_id);
        if(null == uwc){
            return Results.failureWithStatus(7,"User does not exist");
        }
        if(!uwc.getUser_id().equals(user_id) || !userReturnGoodsLog.getUser_id().equals(user_id)){
            return Results.failureWithStatus(8,"User information is inconsistent");
        }
        /**
         * the validation of contact_name
         */
        Object contact_name_obj = reqMap.get("contact_name");
        if(null == contact_name_obj || StringUtil.isEmpty(contact_name_obj.toString())){
            return Results.failureWithStatus(9,"contact_name is null");
        }
        String contact_name = String.valueOf(contact_name_obj);
        /**
         * the validation of contact_mobile
         */
        Object contact_mobile_obj = reqMap.get("contact_mobile");
        if(null == contact_mobile_obj || StringUtil.isEmpty(contact_mobile_obj.toString())){
            return Results.failureWithStatus(10,"contact_mobile is null");
        }
        String contact_mobile = String.valueOf(contact_mobile_obj);
        /**
         * the validation of courier_agency's id
         */
        Object courier_id_obj = reqMap.get("courier_id");
        if(null == courier_id_obj || StringUtil.isEmpty(String.valueOf(courier_id_obj))){
            return Results.failureWithStatus(11,"courier_id is null");
        }
        Integer courier_id = new Integer(String.valueOf(courier_id_obj));
        /**
         * the validation of courier_code
         */
        Object courier_code_obj = reqMap.get("courier_code");
        if(null == courier_code_obj || StringUtil.isEmpty(String.valueOf(courier_code_obj))){
            return Results.failureWithStatus(12,"courier_code is null");
        }
        String courier_code = String.valueOf(courier_code_obj);

        userReturnGoodsLog.setCourier_agency_id(courier_id);
        userReturnGoodsLog.setCourier_code(courier_code);
        userReturnGoodsLog.setContact_mobile(contact_mobile);
        userReturnGoodsLog.setContact_name(contact_name);
        userReturnGoodsLog.setStatus(2);
        int i = userReturnGoodsLogMapper.updateByPrimaryKey(userReturnGoodsLog);
        if(i>0){
            return Results.successWithStatus(0,"操作成功");
        }
        return Results.failureWithStatus(1,"操作失败");
    }

    /**
     * 获取我的订单的退货状态信息
     */
   public  Result getMyOrderStatus(String open_id,Integer user_id,Integer order_id){
       if(StringUtil.isEmpty(open_id)){
           return Results.failure("openId is empty");
       }
       if(null == user_id){
           return Results.failure("userId is empty");
       }
       if(null == order_id){
           return Results.failure("order_id is empty");
       }
       UserOrderDTO userOrderDTO = userOrderMapper.getOneById(order_id);
       if(null == userOrderDTO){
           return Results.failure("该订单不存在");
       }
       if(!userOrderDTO.getUser_id().equals(user_id)){
           return  Results.failure("用户信息不一致,暂不支持退货");
       }
       if(userOrderDTO.getOrder_status() < 5){
           return  Results.failure("该订单未收货或签收,暂不支持退货");
       }
       if(userOrderDTO.getOrder_status() >= 5){
           if(Dates.addHours(userOrderDTO.getUpdate_time(),7*24,null,null).after(new Date())){
               UserReturnGoodsLog urlong = userReturnGoodsLogMapper.find(user_id,open_id,order_id);
               if(null != urlong){
                   if(urlong.getStatus().equals(0)){
                       return Results.success("0","退货申请中");
                   }
                   if(urlong.getStatus().equals(1)){
                       return Results.success("1","同意退货申请");
                   }
                   if(urlong.getStatus().equals(2)){
                       return Results.success("2","用户已提交退货物流信息");
                   }
                   if(urlong.getStatus().equals(3)){
                       return Results.success("3","拒绝退货 ");
                   }
                   if(urlong.getStatus().equals(4)){
                       return Results.success("3","统确认后退货完成 ");
                   }
               }else{
                   return Results.success("99","该订单未退过货");
               }
           }else {
               return  Results.failure("该订单不在退货时间内");
           }
       }
       return Results.failure();
    }

}
